//
//  ViewController.m
//  UIscrollView
//
//  Created by student on 16/2/29.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIPageControl *pagecontroll;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 410, 400    )];
    view1.backgroundColor = [UIColor blueColor];
      UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(410, 0, 410, 400)];
    view2.backgroundColor = [UIColor yellowColor];
      UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(820, 0, 410, 400)];
    view3.backgroundColor = [UIColor redColor];
    UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 200, 410, 400)];
    [scrollview addSubview:view1];
    [scrollview addSubview:view2];
    [scrollview addSubview:view3];
    [self.view addSubview:scrollview];
    
    scrollview.contentSize = CGSizeMake(410*3, 0);
    scrollview.bounces = YES;
    scrollview.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    
    scrollview.pagingEnabled = YES;
    
    scrollview.backgroundColor = [UIColor greenColor];
    
    scrollview.delegate = self;
    
    [self.view addSubview:self.pagecontroll];
    
    self.pagecontroll.numberOfPages = 3;
    self.pagecontroll.currentPage = 0;
    
    self.pagecontroll.pageIndicatorTintColor = [UIColor yellowColor];
    self.pagecontroll.currentPageIndicatorTintColor = [UIColor redColor];
}

// 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%s",__func__);
    // 根据当前内容偏移的位置,计算当前是第几页
    // 内容偏移:contentOffset
    self.pagecontroll.currentPage = scrollView.contentOffset.x/400;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 将要开始拖动
    NSLog(@"%s",__func__);
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    // 将要结束拖动
    NSLog(@"%s",__func__);
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // 已经结束拖动
    NSLog(@"%s",__func__);
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;   // called on finger up as we are moving
{
    // 将要开始减速
    NSLog(@"%s",__func__);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // called when scroll view grinds to a halt
{
    // 已经结束减速
    NSLog(@"%s",__func__);
}

@end
